import {request} from '@/utils/request'
import type { HbtPagedResult, HbtSelectOption } from '@/types/common'
import type { 
  HbtMenuQuery, 
  HbtMenu,
  HbtMenuCreate,
  HbtMenuUpdate,
  HbtMenuStatus,
  HbtMenuOrder
} from '@/types/identity/menu'

/**
 * 获取菜单分页列表
 */
export function getMenuList(params: HbtMenuQuery) {
  return request<HbtPagedResult<HbtMenu>>({
    url: '/api/HbtMenu/list',
    method: 'get',
    params
  })
}

/**
 * 获取菜单详情
 */
export function getByIdAsync(menuId: string) {
  return request<HbtMenu>({
    url: `/api/HbtMenu/${menuId}`,
    method: 'get'
  })
}

/**
 * 创建菜单
 */
export function createMenu(data: HbtMenuCreate) {
  return request<number>({
    url: '/api/HbtMenu',
    method: 'post',
    data
  })
}

/**
 * 更新菜单
 */
export function updateMenu(data: HbtMenuUpdate) {
  return request<boolean>({
    url: '/api/HbtMenu',
    method: 'put',
    data
  })
}

/**
 * 删除菜单
 */
export function deleteMenu(menuId: string) {
  return request<boolean>({
    url: `/api/HbtMenu/${menuId}`,
    method: 'delete'
  })
}

/**
 * 批量删除菜单
 */
export function batchDeleteMenu(menuIds: string[]) {
  return request<boolean>({
    url: '/api/HbtMenu/batch',
    method: 'delete',
    data: menuIds
  })
}

/**
 * 更新菜单状态
 */
export function updateMenuStatus(data: HbtMenuStatus) {
  return request<boolean>({
    url: `/api/HbtMenu/${data.menuId}/status`,
    method: 'put',
    params: { status: data.status }
  })
}

/**
 * 更新菜单排序
 */
export function updateMenuOrder(data: HbtMenuOrder) {
  return request<boolean>({
    url: '/api/HbtMenu/order',
    method: 'put',
    data
  })
}

/**
 * 获取菜单树形结构
 */
export function getMenuTree(params?: HbtMenuQuery) {
  return request<HbtMenu[]>({
    url: '/api/HbtMenu/tree',
    method: 'get',
    params
  })
}

/**
 * 获取当前用户的菜单树
 */
export function getCurrentUserMenus(userId: string) {
  return request<HbtMenu[]>({
    url: `/api/HbtMenu/current-user/${userId}`,
    method: 'get'
  })
}

/**
 * 获取菜单选项列表
 */
export function getMenuOptions() {
  return request<HbtSelectOption[]>({
    url: '/api/HbtMenu/options',
    method: 'get'
  })
}

/**
 * 导入菜单数据
 */
export function importMenu(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  return request<{ success: number; fail: number }>({
    url: '/api/HbtMenu/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 导出菜单数据
 */
export function exportMenu(params?: HbtMenuQuery) {
  return request({
    url: '/api/HbtMenu/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

/**
 * 获取菜单导入模板
 */
export function getTemplate() {
  return request({
    url: '/api/HbtMenu/template',
    method: 'get',
    responseType: 'blob'
  })
} 